﻿
Imports System.Net
Imports System.IO

Public Class WebForm1
    Inherits System.Web.UI.Page

    Shared ReadOnly Property o As IGNOU.StudentAddressCollectorHandler
        Get
            Return Global_asax.IgnouScraperRobot
            'If HttpContext.Current.Application("myrobot") Is Nothing Then
            '    Dim c = New IGNOU.StudentAddressCollectorHandler
            '    HttpContext.Current.Application("myrobot") = c
            'End If
            'Return HttpContext.Current.Application("myrobot")
        End Get
    End Property

    'Shared o As IGNOU.StudentAddressCollectorQueue

    'Shared ReadOnly Property DS As List(Of IGNOU.StudentAddressParser)
    '    Get
    '        If HttpContext.Current.Application("ds") Is Nothing Then
    '            HttpContext.Current.Application("ds") = New List(Of IGNOU.StudentAddressParser)
    '        End If
    '        Return HttpContext.Current.Application("ds")
    '    End Get
    'End Property

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Dim obj = IGNOU.StudentDAL.ListAll(1, 10)

        Dim k As New IGNOU.Student
        With k
            .FullName = "fsad"
            .EmailID = "fads"
        End With

    End Sub

    <Web.Services.WebMethod()> _
    Public Shared Function ListEnrollments(ByVal pageNumber As Integer) As BOResult(Of String)

        Const MAXROWS = 10
        Dim re As New BOResult(Of String)

        Try

            Dim startRow As Integer = pageNumber * MAXROWS

            Dim fs As New System.IO.DirectoryInfo(o.PathToSave)

            Dim allFiles = fs.GetFiles("*.txt", SearchOption.AllDirectories)

            re.TotalRecords = allFiles.Count
            're.TotalRecords = My.Computer.FileSystem.GetFiles(o.PathToSave, FileIO.SearchOption.SearchAllSubDirectories).Count

            re.CurrentPage = pageNumber

            Dim fls = From t In allFiles Order By t.CreationTime Skip startRow Take MAXROWS _
                              Select New IGNOU.StudentAddressParser With {.Data = My.Computer.FileSystem.ReadAllText(t.FullName)}

            Dim lst = fls.ToList

            lst.ForEach(Function(o)
                            o.ParseIt()
                            Return True
                        End Function)

            Dim x = <div>
                        <table>
                            <tr>
                                <th>Enroll</th>
                                <th>Name</th>
                                <th>program</th>
                                <th>Address</th>
                                <th>EmailID</th>
                                <th>Mobile</th>
                                <th>Guardian</th>
                                <th>Status</th>
                            </tr>
                            <%= From t In lst Select <tr>
                                                         <td><%= t.EnrollmentNo %></td>
                                                         <td><%= t.FullName %></td>
                                                         <td><%= t.Program %></td>
                                                         <td><%= t.Address %></td>
                                                         <td><%= t.EmailID %></td>
                                                         <td><%= t.Mobile %></td>
                                                         <td><%= t.Guardian %></td>
                                                         <td><%= t.Status %></td>
                                                     </tr> %></table>
                    </div>


            re.Result = x.ToString
            re.OperationStatus = True
        Catch ex As Exception
            re.OperationStatus = False
            re.Status = "An errror occurred."
        End Try

        Return re
        '        Return x.ToString
    End Function

    <Web.Services.WebMethod()> _
    Public Shared Function AddEnrollment(ByVal e As String) As Boolean
        o.Add(e)
        Return True
    End Function

    <Web.Services.WebMethod()> _
    Public Shared Function AddEnrollmentCSVList(ByVal e As String) As Boolean
        Dim arr = Split(e, ",")
        For Each i In arr
            o.Add(i)
        Next
        Return True
    End Function



    <Web.Services.WebMethod()> _
    Public Shared Function getStudent(ByVal e As String) As IGNOU.StudentAddressParser
        Dim o = IGNOU.IGNOURobot.GetStudent(e)
        Return o
    End Function

    <Web.Services.WebMethod()> _
    Public Shared Function getRobotRunningStatus() As String

        Dim totalProcess = Global_asax.IgnouScraperRobot.TotalProcessed '(From t In My.Computer.FileSystem.GetFiles(o.PathToSave) Select t).Count
        Dim totalUnderProcess = Global_asax.IgnouScraperRobot.TotalPending 'o.Count
        Dim isRunning = Global_asax.IgnouScraperRobot.IsRunning

        Dim x = <div>
                    <table>
                        <tr>
                            <td>Total Processed :</td>
                            <td><%= totalProcess %></td>
                        </tr>
                        <tr>
                            <td>Total Under Processing :</td>
                            <td><%= totalUnderProcess %></td>
                        </tr>
                        <tr>
                            <td>Is Running :</td>
                            <td><%= isRunning %></td>
                        </tr>
                    </table>
                </div>

        Return x.ToString

    End Function

    <Web.Services.WebMethod()> _
    Public Shared Function AddECCodeList(ByVal e As String) As Boolean

        Dim th As New Threading.Thread(AddressOf doADDECCodeListInBackground)
        th.Start(e)
        Return True
    End Function

    Private Shared Sub doADDECCodeListInBackground(ByVal e As String)
        Dim ecCodeArray = Split(e, ",")
        Const URIFormat = "http://others.ignou.ac.in/tedec11/AttendenceHall1211/EXM/E{0}.txt"
        For Each ecCode In ecCodeArray
            Dim urlToGrab = String.Format(URIFormat, ecCode)
            Dim wc As New WebClient
            wc.Proxy = IGNOU.IGNOURobot.prxy
            Dim data = wc.DownloadString(urlToGrab)
            Dim lst = IGNOU.IGNOURobot.ParseEnrollmentNumbers(data)
            For Each enr In lst
                o.Add(enr)
            Next
        Next
    End Sub

End Class

